﻿package log;

import java.io.IOException;
import java.sql.Timestamp;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.jdbc.JDBCAppender;

public class Hello {

    /**
     * 로그 변수 설정.
     * 
     */
    private static final Logger logger = Logger.getLogger(Hello.class);
    /**
     * property를 code내에서 입력하는 소스 구현
     */
    public static Logger myLogger;

    private int num1;
    private int num2;

    public Hello() {
        this.num1 = this.num2 = 0;
    }

    public void setNum1(int num1) {
        if (logger.isDebugEnabled()) {
            logger.debug("setNum1(int) - start");
        }

        this.num1 = num1;

        if (logger.isDebugEnabled()) {
            logger.debug("setNum1(int) - end");
        }
    }

    public void setNum2(int num2) {
        if (logger.isDebugEnabled()) {
            logger.debug("setNum2(int) - start");
        }

        this.num2 = num2;

        if (logger.isDebugEnabled()) {
            logger.debug("setNum2(int) - end");
        }
    }

    public int getSum() {
        if (logger.isDebugEnabled()) {
            logger.debug("getSum() - start");
        }

        int returnint = num1 + num2;
        if (logger.isDebugEnabled()) {
            logger.debug("getSum() - end");
        }
        return returnint;
    }

    public int getNum1() {
        return this.num1;
    }

    /**
     * 입력받은 두번째 인자 반환
     * 
     * @return Hello.num2
     */
    public int getNum2() {
        return this.num2;
    }

    public static Logger getLoggerProperty() {

        Logger logger           = null;
        String pattern          = "[%d{yy-MM-dd HH:mm:ss}] %C %l %m%n";
        String filename         = "myLog.log";
        String loggername       = "myLogger";
        String username         = "name";
        String passwd           = "passwd";
        Level level             = Level.DEBUG;
        boolean additivity      = false;

        try {
            Layout layout = new PatternLayout(pattern);

            /**
             * Db에 로그 저장
             */
            JDBCAppender dbapp = new JDBCAppender();
            dbapp.setLayout(layout);
            dbapp.setUser(username);
            dbapp.setPassword(passwd);
            dbapp.setURL("jdbc:sqlserver://127.0.0.1:1152;DatabaseName=dibida");
            // dbapp.
            dbapp.setSql("insert into hello_log(message, loglevel, route, source, logger, logdate) "
                            + "values ('%d - %c - %p - %m','%p','%C','%F','%c','%d')");
            dbapp.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            dbapp.setBufferSize(1);
            dbapp.setThreshold(level);

            // file.
            FileAppender appender = new FileAppender(layout, filename, true);
            appender.setThreshold(level);

            logger = LogManager.getLogger(loggername);
            logger.addAppender(appender);
            logger.setAdditivity(additivity);
            logger.addAppender(dbapp);

            if (additivity) {
                ConsoleAppender consoleAppender = new ConsoleAppender(layout);
                logger.addAppender(consoleAppender);
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return logger;

    }

    public static void main(String args[]) {
        myLogger = getLoggerProperty();
        if (myLogger.isDebugEnabled()) {
            myLogger.debug("main(String) - start");
        }

        Hello he = new Hello();
        he.setNum1(Integer.parseInt(args[0]));
        he.setNum2(Integer.parseInt(args[1]));

        if (myLogger.isDebugEnabled()) {
            myLogger.debug("input number1 is " + he.getNum1());
            myLogger.debug("input number2 is " + he.getNum2());

        }
        if (myLogger.isDebugEnabled()) {
            myLogger.debug("main(String) - Sum is " + he.getSum());
        }

        if (myLogger.isDebugEnabled()) {
            myLogger.debug("main(String) - end");
        }

        System.exit(0);
    }
}
